# 老师的代码
from time import sleep
from unittest import TestCase

from selenium import webdriver

from teacher.ecshop.dao.MySQLHelper import MySQLHelper
from teacher.ecshop.po.GoodsListPage import GoodsListPage
from teacher.ecshop.po.LoginPage import LoginPage
from teacher.ecshop.utils.Config import Config


class TestGoodsList(TestCase):
    file = './config/config.ini'  # 文件路径为自己的包路径
    config = Config(file)

    def setUp(self) -> None:
        self.driver = webdriver.Chrome(self.config.get_value(self.file, 'system', 'driver'))

    def login(self):
        login = LoginPage(self.driver)
        login.open('http://192.168.20.244/ecshop/admin/privilege.php?act=login')
        login.input_username()
        login.input_password()
        login.click_login_button()

    def goods_list(self):
        goods = GoodsListPage(self.driver)
        goods.into_menu_frame()
        goods.click_goods_link()
        goods.out_menu_frame()
        goods.into_main_frame()
        return goods

    def add_goods(self):
        goods = GoodsListPage(self.driver)
        goods.into_menu_frame()
        goods.click_add_goods_link()
        goods.out_menu_frame()
        goods.into_main_frame()
        return goods

    def test_search_by_keyword(self):
        self.login()
        goods = self.goods_list()
        goods.input_keyword()
        goods.click_search_button()
        sleep(2)

        result = goods.get_search_count()

        mysql = MySQLHelper()
        sql = "select count(*) from ecs_goods where goods_name like '%N85%'"
        rs = mysql.select(sql, 'one')

        # if int(result)==rs[0]:
        #     print('测试通过')
        # else:
        #     print('测试失败')
        # 断言
        self.assertEqual(int(result), rs[0], '测试失败')

    def test_search_by_keyword2type(self):
        self.login()

        goods = self.goods_list()
        goods.select_by_type()
        goods.input_keyword()
        goods.click_search_button()
        sleep(1)
        result = goods.get_search_count()

        mysql = MySQLHelper()
        sql = "select count(*) from ecs_goods where goods_name like '%N85%' and is_new=1"
        rs = mysql.select(sql, 'one')
        self.assertEqual(int(result), rs[0], '测试失败')

    def test_add_goods(self):
        self.login()

        mysql = MySQLHelper()
        sql = "select count(*) from ecs_goods where goods_name ='DiorN85999'"
        old_result = mysql.select(sql, 'one')[0]

        goods = self.add_goods()  # 进入添加商品页
        goods.input_goods_name()
        goods.select_category()
        goods.click_submit()
        sleep(1)
        sql = "select count(*) from ecs_goods where goods_name ='DiorN85999'"
        new_result = mysql.select(sql, 'one')[0]

        self.assertEqual(new_result, old_result + 1, '测试失败')

    def test_is_not_sale(self):
        mysql = MySQLHelper()
        sql = "update ecs_goods set is_on_sale=1 where goods_id=(select max_id from (select max(goods_id) max_id from ecs_goods) as a)"
        mysql.dml(sql)
        sleep(1)

        # 下架
        self.login()
        goods = self.goods_list()
        goods.click_is_not_sales()
        sleep(1)
        sql = "select is_on_sale from ecs_goods where goods_id =(select max_id from (select max(goods_id) max_id from ecs_goods) as a)"
        new_result = mysql.select(sql, 'one')[0]
        self.assertEqual(new_result, 0, '测试失败')
